name: autotests

on:
  pull_request:
  push:
    branches:
      - main

jobs:

  branchtest:
    runs-on: ubuntu-latest

    steps:
      - name: Check branch name
        run: |
          if [[ ! $GITHUB_HEAD_REF =~ ^iter[0-9]+$ ]] && [[ ${{ github.ref }} != "refs/heads/main" ]]; then echo "Branch name must match pattern 'iter<number>' or github.ref must be 'refs/heads/main'" && echo "Your branch is $GITHUB_HEAD_REF and github.ref is ${{ github.ref }}" && exit 1; else echo "Your branch is $GITHUB_HEAD_REF and github.ref is ${{ github.ref }}"; fi

  metricstest:
    runs-on: ubuntu-latest
    container: golang:1.21
    needs: branchtest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: praktikum
        options: >-
          --health-cmd pg_isready
          --health-interval 5s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Download autotests binaries
        uses: robinraju/release-downloader@v1.8
        with:
          repository: Yandex-Practicum/go-autotests
          latest: true
          fileName: "*"
          out-file-path: .tools
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup autotest binary
        run: |
          chmod -R +x $GITHUB_WORKSPACE/.tools
          mv $GITHUB_WORKSPACE/.tools/metricstest /usr/local/bin/metricstest
          mv $GITHUB_WORKSPACE/.tools/random /usr/local/bin/random

      - name: Setup go deps
        run: |
          go install golang.org/x/tools/cmd/goimports@latest


      - name: Build server binary
        run: |
          cd cmd/server
          go build -buildvcs=false -o server

      - name: "Code increment #1"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter1' ||
          github.head_ref == 'iter2' ||
          github.head_ref == 'iter3' ||
          github.head_ref == 'iter4' ||
          github.head_ref == 'iter5' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          metricstest -test.v -test.run=^TestIteration1$ \
            -binary-path=cmd/server/server

      - name: Build agent binary
        run: |
          cd cmd/agent
          go build -buildvcs=false  -o agent

      - name: "Code increment #2"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter2' ||
          github.head_ref == 'iter3' ||
          github.head_ref == 'iter4' ||
          github.head_ref == 'iter5' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          metricstest -test.v -test.run=^TestIteration2[AB]*$ \
            -source-path=. \
            -agent-binary-path=cmd/agent/agent

      - name: "Code increment #3"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter3' ||
          github.head_ref == 'iter4' ||
          github.head_ref == 'iter5' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          metricstest -test.v -test.run=^TestIteration3[AB]*$ \
            -source-path=. \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server

      - name: "Code increment #4"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter4' ||
          github.head_ref == 'iter5' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration4$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #5"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter5' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration5$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #6"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter6' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration6$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #7"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter7' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration7$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #8"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter8' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration8$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #9"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter9' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration9$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -file-storage-path=$TEMP_FILE \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #10"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter10' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration10[AB]$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #11"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter11' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration11$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #12"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter12' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration12$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #13"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter13' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration13$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #14"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter14'
        run: |
          SERVER_PORT=$(random unused-port)
          ADDRESS="localhost:${SERVER_PORT}"
          TEMP_FILE=$(random tempfile)
          metricstest -test.v -test.run=^TestIteration14$ \
            -agent-binary-path=cmd/agent/agent \
            -binary-path=cmd/server/server \
            -database-dsn='postgres://postgres:postgres@postgres:5432/praktikum?sslmode=disable' \
            -key="${TEMP_FILE}" \
            -server-port=$SERVER_PORT \
            -source-path=.

      - name: "Code increment #14 (race detection)"
        if: |
          github.ref == 'refs/heads/main' ||
          github.head_ref == 'iter14'
        run: |
          go test -v -race ./...
